﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using HtmlAgilityPack;

namespace DDE_Server.Object
{
    public class PasserWebPage
    {
        static private PjmInstDispRate disRates = new PjmInstDispRate();
        public PjmInstDispRate DispRate{
            get {
                return disRates;
                }
            //set { }
        }
        public bool Passer( string url) {
            WebClient web = new WebClient();
            string html = "";
            try {
                html = web.DownloadString(url);
            } catch(Exception e)
            {

                return false;
            }
            if (html != "")
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);

                try
                {
                    int tableCount = 0;
                    foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
                    {
                        tableCount++;
                        foreach (HtmlNode row in table.SelectNodes(".//tr"))
                        {

                            foreach (HtmlNode cell in row.SelectNodes("th|td"))
                            {
                                if (tableCount == 1)
                                {
                                    DateTime parsedDate;

                                    String dateStr = cell.InnerText.Replace(" EDT", "");
                                    dateStr = dateStr.Replace(" EST", "");
                                    dateStr = dateStr.Trim();

                                    String pattern = "ddd MMM d HH:mm:ss yyyy";

                                    if (DateTime.TryParseExact(dateStr, pattern, null,
                                        DateTimeStyles.None, out parsedDate))
                                    {
										if (disRates.LastUpdate != parsedDate) {
											disRates.Empty();
											disRates.LastUpdate = parsedDate;
										} else 
										{
											return false;
										}
                                    }
                                    else
                                    {

                                    }

                                    //Thu May 22 12:20:17 EDT 2014
                                }
                                else if (tableCount == 2)
                                {
                                    double price;
                                    String priceStr = cell.InnerText.Trim();
                                    if (double.TryParse(priceStr, out price))
                                    {

                                        disRates.updateRateTable(price);
                                    }
                                }
                                else
                                {
                                    //Console.WriteLine("nothing");
                                }

                            }
                        }
                    }
					//disRates.SortRate();
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e.Message);
                    return false;
                }
            }
            else
            {
                return false;
            }

            return true;
        }
    }
}
